package com.zzedu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzedu.common.annotation.Log;
import com.zzedu.common.core.controller.BaseController;
import com.zzedu.common.core.domain.AjaxResult;
import com.zzedu.common.core.page.TableDataInfo;
import com.zzedu.common.enums.BusinessType;
import com.zzedu.common.enums.CommonEnums;
import com.zzedu.common.utils.ServletUtils;
import com.zzedu.common.utils.poi.ExcelUtil;
import com.zzedu.domain.ZzPictureBook;
import com.zzedu.dto.ZzPictureBookDTO;
import com.zzedu.params.ZzPictureBookParams;
import com.zzedu.service.IZzPictureBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 绘本管理Controller
 *
 * @author Mr.Wang
 * @date 2025-04-04
 */
@RestController
@RequestMapping("/course/picturebook")
public class ZzPictureBookController extends BaseController {
    @Autowired
    private IZzPictureBookService zzPictureBookService;

    /**
     * 查询绘本管理列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('course:picturebook:query')")
    @RequestMapping("/list")
    public TableDataInfo list(@RequestBody ZzPictureBookDTO zzContentHead) {
        IPage<ZzPictureBook> page = zzPictureBookService.page(zzContentHead.getPageDTO(), zzPictureBookService.buildQuery(zzContentHead));
        return getDataTable(page, zzPictureBookService.convertVO(page.getRecords()));
    }

    /**
     * 导出绘本管理列表
     */
    @PreAuthorize("@permission.hasAnyPermissions('course:picturebook:export')")
    @Log(title = "绘本管理", businessType = BusinessType.EXPORT)
    @RequestMapping("/export")
    public void export(ZzPictureBookDTO zzContentHead) {
        List<ZzPictureBook> list = zzPictureBookService.list(zzPictureBookService.buildQuery(zzContentHead));
        ExcelUtil<ZzPictureBook> util = new ExcelUtil<ZzPictureBook>(ZzPictureBook.class);
        util.exportExcel(ServletUtils.getResponse(), list, "绘本管理数据");
    }


    /**
     * 新增保存绘本管理
     */
    @PreAuthorize("@permission.hasAnyPermissions('course:picturebook:add')")
    @Log(title = "绘本管理", businessType = BusinessType.INSERT)
    @RequestMapping("/add")
    public AjaxResult addSave(@Validated ZzPictureBookParams zzPictureBookParams) throws Exception {
        return toAjax(zzPictureBookService.save(zzPictureBookParams));
    }


    /**
     * 获取绘本管理详细信息
     */
    @PreAuthorize("@permission.hasAnyPermissions('course:picturebook:query')")
    @RequestMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(zzPictureBookService.selectById(id));
    }


    /**
     * 修改保存绘本管理
     */
    @PreAuthorize("@permission.hasAnyPermissions('course:picturebook:edit')")
    @Log(title = "绘本管理", businessType = BusinessType.UPDATE)
    @RequestMapping("/edit")
    public AjaxResult editSave(@Validated ZzPictureBookParams zzPictureBookParams) throws Exception {
        zzPictureBookParams.setState(null);
        return toAjax(zzPictureBookService.save(zzPictureBookParams));
    }

    /**
     * 删除绘本管理
     */
    @PreAuthorize("@permission.hasAnyPermissions('course:picturebook:remove')")
    @Log(title = "绘本管理", businessType = BusinessType.DELETE)
    @RequestMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(zzPictureBookService.removeBatchByIds(ids));
    }

    /**
     * 绘本上线/下线
     */
    @PreAuthorize("@permission.hasAnyPermissions('course:picturebook:online,course:picturebook:offline')")
    @Log(title = "绘本管理", businessType = BusinessType.UPDATE)
    @RequestMapping("/online/{ids}")
    public AjaxResult online(@PathVariable List<Long> ids) {
        return toAjax(zzPictureBookService.update(Wrappers.lambdaUpdate(ZzPictureBook.class)
                .set(ZzPictureBook::getState, CommonEnums.YES.getCode())
                .in(ZzPictureBook::getId, ids)));
    }

    /**
     * 绘本下线
     */
    @PreAuthorize("@permission.hasAnyPermissions('course:picturebook:edit')")
    @Log(title = "绘本管理", businessType = BusinessType.UPDATE)
    @RequestMapping("/offline/{ids}")
    public AjaxResult offline(@PathVariable List<Long> ids) {
        return toAjax(zzPictureBookService.update(Wrappers.lambdaUpdate(ZzPictureBook.class)
                .set(ZzPictureBook::getState, CommonEnums.NO.getCode())
                .in(ZzPictureBook::getId, ids)));
    }
}
